<?php
namespace app\models;

use Yii;
use yii\base\Model;
use yii\db\ActiveRecord;
/**
 * This is the model class for table "menu".
 *
 * @property integer $id Menu id(autoincrement)
 * @property string $name Menu name
 * @property integer $parent Menu parent
 * @property string $route Route for this menu
 * @property integer $order Menu order
 * @property string $data Extra information for this menu
 *
 * @property Menu $menuParent Menu parent
 * @property Menu[] $menus Menu children
 *
 * @author Misbahul D Munir <misbahuldmunir@gmail.com>
 * @since 1.0
 */
class Menu extends ActiveRecord
{
    public $parent_name;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return "bk_admin_menu";
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['parent_id'], 'filterParent'],
            [['parent_name'], 'in',
                'range' => static::find()->select(['name'])->column(),
                'message' => 'Menu "{value}" not found.'],
            [['parent_id', 'route', 'data', 'order'], 'default'],
            [['order'], 'integer'],
            [['route'], 'in',
                'range' => static::getSavedRoutes(),
                'message' => 'Route "{value}" not found.']
        ];
    }

    /**
     * Use to loop detected.
     */
    public function filterParent()
    {
        $value = $this->parent_id;
        $parent = self::findOne(['menu_id' => $value]);
        if ($parent) {
            $menu_id = $this->menu_id;
            $parent_id = $parent->menu_id;
            $parent_name = $parent->name;
            while ($parent) {
                if ($parent->menu_id == $menu_id) {
                    $this->addError('parent_name', 'Loop detected.');
                    return;
                }
                $parent = $parent->menuParent;
            }
            $this->parent_id = $parent_id;
            $this->parent_name = $parent_name;
        } else {
            $this->parent_id = null;
            $this->parent_name = null;
        }
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'menu_id' => 'ID',
            'name' => 'Name',
            'parent_id' => 'Parent ID',
            'parent_name' => 'Parent Name',
            'route' => 'Route',
            'order' => 'Order',
            'data' => 'Data',
        ];
    }
    /**
     * Get menu parent
     * @return \yii\db\ActiveQuery
     */
    public function getMenuParent()
    {
        return $this->hasOne(Menu::className(), ['menu_id' => 'parent_id']);
    }
    /**
     * Get menu children
     * @return \yii\db\ActiveQuery
     */
    public function getMenus()
    {
        return $this->hasMany(Menu::className(), ['parent_id' => 'menu_id']);
    }
    /**
     * Get saved routes.
     * @return array
     */
    public static function getSavedRoutes()
    {
        $result = [];
        foreach (Yii::$app->getAuthManager()->getPermissions() as $name => $value) {
            if ($name[0] === '/' && substr($name, -1) != '*') {
                $result[] = $name;
            }
        }
        return $result;
    }
}
